home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / PublicDomain / Anwendungen / QuickFile / ARexx / FindReplace.quickfile < prev    next >
Text File  |  1995-11-19  |  4KB  |  218 lines

  1. /*
  2.     FindReplace.QuickFile
  3.  
  4.     Replaces an existing string with a new string in all records
  5.     in the current index (or selection)
  6.  
  7.     Author: Alan Wigginton
  8.     Date:   July 1995
  9. */
  10.  
  11. options results
  12.  
  13. if ~open(console,"con:20/40/460/120/SetField","W") then
  14. do
  15.     say "Could not open window"
  16.     exit(20)
  17. end
  18.  
  19. "query field fld"       /* get all the field names in stem fld. */
  20.  
  21. "setindex"              /* get the current index name   */
  22. ixname = result
  23.  
  24. "query index ixfld '"ixname"'"  /* get the index field names    */
  25. if rc > 0 then
  26.     call ErrorProc "Error getting index details"
  27.  
  28. call writeln(console, "Find and replace string")
  29. call writeln(console, "Press RETURN with no value to exit")
  30.  
  31. /*
  32.     Get the field name. Keep trying until the user gets it right
  33.     or they give up (RETURN with no data)
  34. */
  35.  
  36. err = "Y"
  37. do while err = "Y"
  38.     call writeln(console, "What is the field to be changed?")
  39.     ReqFld = upper(readln(console))
  40.     if ReqFld = "" then
  41.     exit
  42.     call CheckFld
  43. end
  44.  
  45. call CheckIndex
  46.  
  47. /*
  48.     Get the value to set the field to
  49. */
  50.  
  51. err = "Y"
  52. do while err = "Y"
  53.     call writeln(console, "Search for what?")
  54.     SrchStr = readln(console)
  55.     if SrchStr ~= "" then
  56.     err = "N"
  57.     else
  58.        call writeln(console, "Must have a search string")
  59. end
  60.  
  61. call GetYesNo "Match case y/n?", "n"
  62. CaseSens = result
  63.  
  64. call writeln(console, "Replace" SrchStr "with what?")
  65. RepStr = readln(console)
  66.  
  67. /*
  68.     Confirm operation and number of records to be updated
  69. */
  70.  
  71. "numrecs"       /* ask quickfile for the number of records */
  72. numrecs = result
  73.  
  74. if ixname = "Selected" then
  75.     ixtype = "Selection"
  76. else
  77.     ixtype = "all"
  78.  
  79. call writeln(console, "About to replace" SrchStr)
  80. call writeln(console, "with" RepStr "in" ixtype "records")
  81.  
  82. call GetYesNo "Continue y/n?", "y"
  83.  
  84. if result ~= "Y" then
  85. do
  86.     call writeln(console, "Request cancelled - press any key")
  87.     ans = readln(console)
  88.     exit
  89. end
  90.  
  91. /*
  92.     Change all the records
  93. */
  94.  
  95. count = 0
  96. srchlen = length(SrchStr)
  97. replen    = length(RepStr)
  98. if CaseSens = "N" then
  99.     SrchStr = upper(SrchStr)
  100.  
  101. "next -9999"
  102. eof = "N"
  103. do while eof = "N"
  104.     "getfield '"ReqFld"'"
  105.  
  106.     if CaseSens = "N" then
  107.     pos = Index(upper(result), SrchStr)
  108.     else
  109.     pos = Index(result, SrchStr)
  110.  
  111.     if pos > 0 then
  112.     do
  113.     newstr = delstr(result, pos, srchlen)
  114.     newstr = insert(RepStr, newstr, pos - 1)
  115.  
  116.     "putfield '"ReqFld"' '"newstr"'"
  117.     if rc > 0 then
  118.     do
  119.         call writeln(console, "Error placing" newstr "into" ReqFld)
  120.         call ErrorProc count "records updated"
  121.     end
  122.     "updrec"        /* commit the updated record     */
  123.     if rc > 0 then
  124.         call ErrorProc  "Error updating record"
  125.     count = count + 1
  126.     end
  127.     "next"
  128.     if rc > 0 then
  129.     eof = "Y"
  130. end
  131.  
  132. /*
  133.     Tell the user that we completed it OK
  134. */
  135.  
  136. call writeln(console, count "records updated")
  137. call writeln(console, "Press RETURN to continue")
  138. ans = readln(console)
  139. exit
  140.  
  141.  
  142. /*
  143.     Check that the field name is valid
  144. */
  145.  
  146. CheckFld:
  147.  
  148. do i = 1 to fld.0
  149.     if ReqFld = fld.i then
  150.     do
  151.     err = "N"
  152.     return
  153.     end
  154. end
  155.  
  156. call writeln(console, ReqFld "is unknown")
  157.  
  158. return
  159.  
  160. CheckIndex:
  161.  
  162. ans = "Y"
  163. do i = 1 to ixfld.0
  164.     if ReqFld = ixfld.i then
  165.     ans = "N"
  166. end
  167.  
  168. if ans = "N" then
  169. do
  170.     call writeln(console, ReqFld "is used in current index")
  171.     call writeln(console, "results may be unpredictable")
  172.     call writeln(console, "Best use another key or sort the file first")
  173.     call GetYesNo "Continue anyway y/n?", "n"
  174.     if result = "N" then
  175.     call ErrorProc "Request cancelled"
  176. end
  177.  
  178. return
  179.  
  180.  
  181. /*
  182.     Ask for a Y or N response. Nothing else will be accepted
  183. */
  184.  
  185. GetYesNo:
  186.  
  187. parse arg msg, default
  188.  
  189. err = "Y"
  190. do while err = "Y"
  191.     call writeln(console, msg "["default"]")
  192.     ans = upper(readln(console))
  193.     if ans = "" then
  194.     ans = upper(default)
  195.     if ans = "Y" | ans = "N" then
  196.     err = "N"
  197.     else
  198.     call writeln(console, "Answer y or n")
  199. end
  200.  
  201. return ans
  202.  
  203.  
  204. /*
  205.     Display error message and exit
  206.     ------------------------------
  207. */
  208.  
  209. ErrorProc:
  210.  
  211. arg errmsg
  212.  
  213. call writeln(console, errmsg)
  214. call writeln(console, "Press RETURN to continue")
  215. ans = readln(console)
  216. exit(10)
  217.  
  218.